<?php

namespace app\index\controller;

use app\common\controller\IndexBase;
use app\common\model\Carrier;
use app\common\model\Cart;
use app\common\model\Country;
use app\common\model\Goods;
use app\common\model\GoodsImg;
use app\common\model\GoodsMain;
use app\common\model\Address as addressModel;
use app\common\model\Country as countryModel;
use app\common\model\Parts;
use app\common\model\Service;

class Ajax extends IndexBase
{
    public function initialize()
    {
        parent::initialize(); // TODO: Change the autogenerated stub
    }

    // 获取颜色规格图片
    public function getPic()
    {
        $data = safeInput();
        $list = GoodsImg::where('goods_id',$data['goods_id'])
            ->where('goods_property_val_id',$data['val_id'])
            ->select();
        if ($list->isEmpty()) {
            return $this->error();
        } else {
            return $this->successData($list);
        }
    }

    // 获取产品价格
    public function getPrice()
    {
        $val_id = safeInput('val_id');
        $goods_id = safeInput('goods_id');
        sort($val_id);
        $str = join("_",$val_id);
        $main = GoodsMain::where("pv_id",$str)
            ->where('goods_id',$goods_id)
            ->find();
        $price = $main ? $main: [
            "stock" => 0,
            "my_price" => 0
        ];
        return $this->successData($price);
    }

    // 获取物流
    public function getCarrier()
    {
        $zip = safeInput('zip');
        $list = Carrier::where("country_id",session('country.id'))
            ->where('zip',$zip)
            ->select();
        if ($list->isEmpty()) {
            return  $this->error();
        }
        return $this->successData($list);
    }
    // 获取用户地址列表
    public function getAddress()
    {
        $list = addressModel::where('user_id',$this->user->id)->select();
        return $this->successData($list);
    }
    public function getCountryList()
    {
        $list = countryModel::select();
        return $this->successData($list);
    }
    public function getCarrierList()
    {
        $data = safeInput();
        $list = Carrier::where('country_id',$data['country_id'])
            ->where('zip','in',$data['zip'])
            ->select();
        if ($list->isEmpty()) {
            $list = Carrier::where('country_id',$data['country_id'])
                ->where('zip',9999999)
                ->select();
        }
        return $this->successData($list);
    }
    // 获取购物车所有物品的价格
    public function getAllCartPrice()
    {
        $country = safeInput('country_id');
        $country = Country::where("id",$country)->find();
        session('country',$country);
        $cartList = Cart::where("user_id",$this->user->id)
            ->with('goods')
            ->select()->toArray();
        $price = 0;
        $weight = 0;
        foreach ($cartList as $item) {
            $price += $item['my_price'] * $item['num'];
            $goods = unserialize($item['body']);
            $weight += $goods['goods']["weight"] * $item['num'];
        }
        return $this->successData([
            "weight" => round($weight,1),
            "price"=>$price,
            "list" =>$cartList
        ]);
    }

    // 获取单个物品购买的价格
    public function getSingleCartPrice()
    {
        $country = safeInput('country_id');
        $country = Country::where("id",$country)->find();
        session('country',$country);
        $cart = session('singleCart');

        $goods = Goods::find($cart['goods_id']);

        $base_price = $goods->my_price;
        if (!empty($cart['goods_main_id'])) {
            $main = GoodsMain::find($cart['goods_main_id']);
            $base_price = $main->my_price;
        }
        $parts_id = $cart["parts_id"];
        if (empty($parts_id)) {
            $parts_price = 0;
        } else {
            $parts = Parts::where("id","in",$parts_id)->select()->toArray();
            $parts_price = array_column($parts,"my_price");
            $parts_price = array_sum($parts_price);
        }

        $service_id = $cart["service_id"];
        if (empty($service_id)) {
            $service_price = 0;
        } else {
            $service =  Service::where("id","in",$service_id)->select()->toArray();
            $service_price = array_column($service,"my_price");
            $service_price = array_sum($service_price);
        }
        $price = $base_price + $parts_price + $service_price;
        $cart["my_price"] = $price;
        session('singleCart',$cart);
        return $this->successData([
            "weight" => round($goods->weight * $cart['num'],1),
            "price"=>$price * $cart['num'],
            "list" =>$cart
        ]);
    }
}